<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户注册页面</title>
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        html,
        body {
            height: 100%;
            width: 100%;
            overflow: hidden;
            background: url(img/bg4.jpg) no-repeat center;
            background-size: 100% 100%;
        }
        
        .box {
            width: 450px;
            height: 500px;
            margin: 30px auto;
            position: relative;
        }
        
        .wrap {
            width: 450px;
            height: 500px;
            margin: 30px auto;
            border-radius: 10px;
            background-color: rgba(255, 255, 255, .8);
        }
        
        .wrap h3 {
            margin: 0;
            height: 60px;
            line-height: 60px;
            font-family: "华文细黑";
            border-bottom: 1px solid #999999;
            text-indent: 16px;
        }
        
        .wrap form {
            padding: 12px;
            box-sizing: border-box;
        }
        
        .register {
            margin: 0 100px;
        }
        
        .false {
            font-size: 26px;
            position: absolute;
            top: 10px;
            right: 20px;
            cursor: pointer;
            transform: scale(1);
        }
        
        .false:hover {
            transform: scale(1.1);
            transition: transform .6s;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="wrap">
            <h3>用户注册</h3>
            <form>
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" class="form-control" id="username" placeholder="请输入用户名">
                </div>
                <div class="form-group">
                    <label for="realName">真实姓名</label>
                    <input type="text" class="form-control" id="realname" placeholder="请输入真实姓名">
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" class="form-control" id="password" placeholder="请输入密码">
                </div>
                <div class="form-group">
                    <label for="repassword">确认密码</label>
                    <input type="password" class="form-control" id="repassword" placeholder="请再次输入密码">
                </div>
                <div class="form-group">
                    <label class="radio-inline">
                    <input type="radio" name="type" id="type-stu" checked>学生
                </label>
                    <label class="radio-inline">
                    <input type="radio" name="type" id="type-teacher">老师
                </label>
                </div>
                <button type="button" class="btn btn-primary register">注册</button>
                <button type="button" class="btn btn-danger cancel">取消</button>
            </form>
        </div>
        <span class="false">×</span>
    </div>
    <script src="lib/jquery.min.js"></script>
    <script src="lib/common.js"></script>
    <script>
        $(".register").click(function() {
            if ($("#username").val() && $("#realname").val() && $("#password").val() && $("#repassword").val()) {
                if ($("#password").val() === $("#repassword").val()) {
                    $.ajax({
                        url: `${address}/register`,
                        method: "POST",
                        data: {
                            username: $("#username").val(),
                            password: $("#password").val(),
                            usertype: $("#type-teacher")[0].checked ? 0 : 1,
                            realname: $("#realname").val(),
                        },
                        success(data) {
                            if (data === "error") {
                                alert("注册失败！");
                            } else {
                                location.href = `${address}/login.html`;
                            }
                        }
                    })
                } else {
                    alert("两次输入密码不一致！");
                }
            } else {
                alert("请输入完整信息！");
            }
        })
        $(".cancel").click(function() {
            location.href = `${address}/login.html`
        })
        $(".false").click(function() {
            location.href = `${address}/login.html`
        })

        //回车实现登录效果
        window.onkeydown = function(event) {
            if (event.keyCode === 13) {
                $(".register").click();
            }
        }
    </script>
</body>

</html>